package com.harmontronics.hanma.paas.paas.token;

import com.harmontronics.hanma.paas.paas.exception.TokenErrorException;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import com.harmontronics.mos.sdk.JwtParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ldb
 */
@Component("mosTokenKit")
public class MosTokenKit implements TokenKit, TokenRenew {


    @Override
    public String getTokenHashKey(Long id) {
        throw new UnsupportedOperationException();
    }

    @Override
    public StringRedisTemplate getRedisTemplate() {
        throw new UnsupportedOperationException();
    }

    @Override
    public TokenKit getTokenKit() {
        return this;
    }

    @Override
    public String getTokenKey() {
        HttpServletRequest request = RequestUtils.getRequest();
        if (request == null) {
            return null;
        }
        String token = StringUtils.trimWhitespace(request.getHeader("Access-Token"));
        return StringUtils.isEmpty(token) ? StringUtils.trimWhitespace(request.getParameter("Access-Token")) : token;
    }

    @Override
    public Long getId() {
        return JwtParser.getId(getTokenKey());
    }

    @Override
    public Boolean getPreview() {
        throw new UnsupportedOperationException();
    }


    @Override
    public UserInfo renew() {
       throw new UnsupportedOperationException("请使用MOS-SDK的TOKEN刷新拦截器");
    }

    @Override
    public UserInfo getUserInfo() {
        String token = getTokenKey();
        Long id = JwtParser.getId(token);
        Boolean admin = JwtParser.getIsAdmin(token);
        String username = JwtParser.getUsername(token);
        String jobNumber = JwtParser.getJobNumber(token);
        return new UserInfo(id,1L,"mos",admin,username,jobNumber);
    }
}
